package cn.flying.cloud.oauth.server.configuration.authentication;

import java.util.Collection;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

/**
 * 验证码授权模式登录
 *
 * @author: admin
 * @date: 2024年05月16日 14:47
 * @version: 1.0
 */
public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {

    // 认证主体信息，认证之前是手机号或者邮箱，认证成功后是UserDetails
    private final Object principal;
    private String credentials;

    /**
     * 创建一个未认证对象
     *
     * @param principal
     * @param credentials
     */
    public SmsCodeAuthenticationToken(Object principal, String credentials) {
        super(null);
        this.principal = principal;
        this.credentials = credentials;
        setAuthenticated(false);
    }

    /**
     * Creates a token with the supplied array of authorities.
     *
     * @param authorities the collection of <tt>GrantedAuthority</tt>s for the principal
     *                    represented by this authentication object.
     */
    public SmsCodeAuthenticationToken(Collection<? extends GrantedAuthority> authorities, Object principal, String credentials) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        super.setAuthenticated(true);
    }

//    /**
//     * @param authenticated <code>true</code> if the token should be trusted (which may
//     *                      result in an exception) or <code>false</code> if the token should not be trusted
//     */
//    @Override
//    public void setAuthenticated(boolean authenticated) {
//        super.setAuthenticated(authenticated);
//    }

    /**
     * The credentials that prove the principal is correct. This is usually a password,
     * but could be anything relevant to the <code>AuthenticationManager</code>. Callers
     * are expected to populate the credentials.
     *
     * @return the credentials that prove the identity of the <code>Principal</code>
     */
    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    /**
     * The identity of the principal being authenticated. In the case of an authentication
     * request with username and password, this would be the username. Callers are
     * expected to populate the principal for an authentication request.
     * <p>
     * The <tt>AuthenticationManager</tt> implementation will often return an
     * <tt>Authentication</tt> containing richer information as the principal for use by
     * the application. Many of the authentication providers will create a
     * {@code UserDetails} object as the principal.
     *
     * @return the <code>Principal</code> being authenticated or the authenticated
     * principal after authentication.
     */
    @Override
    public Object getPrincipal() {
        return this.principal;
    }
}
